package avicit.bdp.dds.server.worker.task.ft;

import avicit.bdp.common.utils.ConfigUtils;
import avicit.bdp.core.http.HttpClientUtil;
import avicit.bdp.core.http.common.HttpConfig;
import avicit.bdp.core.util.json.JSONUtils;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据处理API接口工具类
 *
 * @author xugb
 * @date 2023/6/26 16:37
 */
public class FTUtils {
    private static final Logger logger = LoggerFactory.getLogger(FTUtils.class);

    /**
     * 启动数据处理任务
     *
     * @param formatJson formatJson
     * @return boolean
     */
    public static boolean startTask(String formatJson) {
        if (StringUtils.isBlank(formatJson)) {
            return false;
        }

        logger.info("接口超时时间1000s");
        logger.info("start flow, format json: \n {}", formatJson);
        String server = ConfigUtils.getInstance().getString("ft.server");
        String startUrl = server + "/api/dh/task/startTask";
        try {
            long startTime = System.currentTimeMillis();
            HttpConfig config = HttpConfig.custom().timeout(1000 * 1000).url(startUrl).json(formatJson);
            String doPostResult = HttpClientUtil.post(config);
            logger.info("接口调用耗时:{}ms", System.currentTimeMillis() - startTime);

            logger.info("Return information：{}", doPostResult);
            if (StringUtils.isNotBlank(doPostResult)) {
                // Convert a json string to a json object
                JSONObject obj = JSONObject.parseObject(doPostResult).getJSONObject("responseBody");
                int stat = obj.getIntValue("stat");
                if (stat == 1) {
                    return true;
                }
            }

        } catch (Exception e) {
            logger.error("start task error: ", e);
            throw new RuntimeException(e);
        }
        return false;
    }
    /**
     * 停止任务
     *
     * @param	id
     * @return boolean
     *
     */
    public static boolean stopTask(String id) {
        if (StringUtils.isBlank(id)) {
            return false;
        }

        logger.info("接口超时时间1000s");
        logger.info("stop task, id: \n {}", id);
        String server = ConfigUtils.getInstance().getString("ft.server");
        String startUrl = server + "/api/dh/task/stopTask";
        try {
            Map queryMap = new HashMap<>();
            queryMap.put("id", id);
            long startTime = System.currentTimeMillis();
            HttpConfig config = HttpConfig.custom().timeout(1000 * 1000).url(startUrl).json(JSONUtils.toJson(queryMap));
            String doPostResult = HttpClientUtil.post(config);
            logger.info("接口调用耗时:{}ms", System.currentTimeMillis() - startTime);

            logger.info("Return information：{}", doPostResult);
            if (StringUtils.isNotBlank(doPostResult)) {
                // Convert a json string to a json object
                JSONObject obj = JSONObject.parseObject(doPostResult).getJSONObject("responseBody");
                int stat = obj.getIntValue("stat");
                if (stat == 1) {
                    return true;
                }
            }

        } catch (Exception e) {
            logger.error("stop task error: ", e);
            throw new RuntimeException(e);
        }
        return false;
    }
    /**
     * 查询任务状态
     *
     * @param	id
     * @return int
     *
     */
    public static int getTaskStatus(String id) {

        logger.info("接口超时时间1000s");
        logger.info("getTaskStatus, id: \n {}", id);
        String server = ConfigUtils.getInstance().getString("ft.server");
        String startUrl = server + "/api/dh/task/getTaskStatus?id=" + id;
        //状态: 0-初始化,1-处理开始,7-处理成功,6-处理失败,9-停止处理
        //默认是处理失败
        int stat = 6;
        try {

            long startTime = System.currentTimeMillis();
            HttpConfig config = HttpConfig.custom().timeout(1000 * 1000).url(startUrl);
            String doPostResult = HttpClientUtil.get(config);
            logger.info("接口调用耗时:{}ms", System.currentTimeMillis() - startTime);

            logger.info("Return information：{}", doPostResult);
            if (StringUtils.isNotBlank(doPostResult)) {
                // Convert a json string to a json object
                JSONObject obj = JSONObject.parseObject(doPostResult).getJSONObject("responseBody");
                stat = obj.getIntValue("stat");
            }

        } catch (Exception e) {
            logger.error("stop task error: ", e);
            throw new RuntimeException(e);
        }
        return stat;
    }

}
